package com.supplychainapi.service.impl;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbtreetypeMapper;
import com.supplychainapi.model.ScmTbtreetype;
import com.supplychainapi.service.IScmTbtreebuildTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class IScmTbtreebuildTypeServiceImpl  implements IScmTbtreebuildTypeService {


    @Autowired
    ScmTbtreetypeMapper tbtreetypeMapper;


    @Override
    public ScmTbtreetype selectByType(ScmTbtreetype model) throws ServiceException {
        return tbtreetypeMapper.selectOne(model);
    }

    @Override
    public boolean save(ScmTbtreetype model) throws ServiceException{
        ScmTbtreetype result= tbtreetypeMapper.selectOne(model);
        if(null!=result){
            if(model.getFiClassLvl()>result.getFiClassLvl()){
                throw ExceptionMessage.tree_type_lv_error.getServiceException();
            }
        }
        model.setFsUpdateTime(new Date());
        int i = tbtreetypeMapper.insertSelective(model);
        return i > 0;
    }

    @Override
    public boolean update(ScmTbtreetype model) throws ServiceException{
        ScmTbtreetype result= tbtreetypeMapper.selectOne(model);
        if(null!=result){
            if(model.getFiClassLvl()>result.getFiClassLvl()){
                throw ExceptionMessage.tree_type_lv_error.getServiceException();
            }
        }
        int i =tbtreetypeMapper.updateByPrimaryKeySelective(model);
        return i > 0;
    }

    @Override
    public boolean delete(ScmTbtreetype model) throws ServiceException{
        int i=tbtreetypeMapper.deleteByPrimaryKey(model);
        return i > 0;
    }
}
